package com.haogu.service.impl;

import com.haogu.mapper.UserMapper;
import com.haogu.pojo.User;
import com.haogu.pojo.vo.ValidateLoginVo;
import com.haogu.service.UserService;
import com.haogu.utils.RandomUtils;
import com.haogu.utils.SMSUtils;
import com.haogu.utils.ValidateCodeUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SMSUtils smsUtils;



//    @Override
//    public Map<String, String> validateCode() {
//        ValidateCodeUtil.Validate validate = ValidateCodeUtil.getRandomCode();
//        String code = validate.getBase64Str();
//        String client = UUID.randomUUID().toString();
//        client = client.replace("-" , "");
//        client += System.currentTimeMillis();
//        Map<String, String> map = new HashMap<>();
//        map.put("code" , code);
//        map.put("client" , client);
//        redisTemplate.opsForValue().set(client , validate.getValue());
//        redisTemplate.expire(client , 1 , TimeUnit.HOURS);
//        System.out.println(validate.getValue());
//        return map;
//    }

//    账号密码登录
    @Override
    public String login(ValidateLoginVo validateLoginVo) {
        if (!StringUtils.hasText(validateLoginVo.getPhone())){
            throw new RuntimeException("账号不能为空");
        }
        if (!StringUtils.hasText(validateLoginVo.getPassword())){
            throw new RuntimeException("密码不能为空");
        }

        User user = userMapper.findByPhone(validateLoginVo.getPhone());
        if (user == null){
            throw new RuntimeException("账户不存在");
        }
        if (!validateLoginVo.getPassword().equals(user.getPassword())){
            throw new RuntimeException("密码错误");
        }
        String token = UUID.randomUUID().toString();
        token = token.replace("-","");
        token = "nekot" + token;
        redisTemplate.opsForValue().set(token,user.getId());
        redisTemplate.opsForValue().set(user.getId() , token);
        redisTemplate.expire(token,1,TimeUnit.DAYS);
        return token;
    }

//    账号验证码登录
    @Override
    public String loginVerificationCode(ValidateLoginVo validateLoginVo) {
        if (!StringUtils.hasText(validateLoginVo.getPhone())){
            throw new RuntimeException("账号不能为空");
        }
        String code = (String) redisTemplate.opsForValue().get(validateLoginVo.getPhone());
        if (!StringUtils.hasText(code)){
            throw new RuntimeException("验证码失效了");
        }
        if (!code.equalsIgnoreCase(validateLoginVo.getCode())){
            throw new RuntimeException("验证码不正确");
        }
        User user = userMapper.findByPhone(validateLoginVo.getPhone());
        if (user == null){
            throw new RuntimeException("账户不存在");
        }
        String token = UUID.randomUUID().toString();
        token = token.replace("-","");
        token = "nekot" + token;
        redisTemplate.opsForValue().set(token,user.getId());
        redisTemplate.opsForValue().set(user.getId() , token);
        redisTemplate.expire(token,1,TimeUnit.DAYS);
        return token;
    }

//    发送验证码
    @Override
    public String validateMessagesCode(String phone) throws Exception {
        if (!StringUtils.hasText(phone)){
            throw new RuntimeException("电话号码不能为空");
        }
        String code = RandomUtils.randomNumber(4);
        redisTemplate.opsForValue().set(phone,code);
//        smsUtils.senderSms(phone, code);
        redisTemplate.expire(phone , 5 , TimeUnit.MINUTES);
        return code;
    }

//    注册账号
    @Override
    public String Messages(ValidateLoginVo validateLoginVo) {
        if (!StringUtils.hasText(validateLoginVo.getPhone())){
            throw new RuntimeException("手机号不能为空");
        }
        if (!StringUtils.hasText(validateLoginVo.getCode())){
            throw new RuntimeException("验证码不能为空");
        }
        if (!StringUtils.hasText(validateLoginVo.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        String code = (String) redisTemplate.opsForValue().get(validateLoginVo.getPhone());
        if (!StringUtils.hasText(code)){
            throw new RuntimeException("验证码失效了");
        }
        if (!code.equalsIgnoreCase(validateLoginVo.getCode())){
            throw new RuntimeException("验证码不正确");
        }
        User user = userMapper.findByPhone(validateLoginVo.getPhone());
        if (user != null){
            throw new RuntimeException("账号已存在,直接登录就行");
        }
        user = new User();
        user.setPhone(validateLoginVo.getPhone());
        user.setPassword(validateLoginVo.getPassword());
        userMapper.add(user);
        return "注册成功";
    }
}
